<template>
  <div class="mainua">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>新增客服</span>
          </div>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l"></div>
        </div>
        <div class="maincla_form">
          <el-form
            ref="form"
            :model="detail"
            :rules="rules"
            label-width="120px"
          >
            <el-form-item label="姓名" prop="name">
              <el-input v-model="detail.name"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="detail.mobile" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="detail.email" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="昵称" prop="nickname">
              <el-input v-model="detail.nickname" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="客户角色" prop="cust_role">
              <el-select v-model="detail.cust_role" placeholder="客户角色">
                <el-option
                  v-for="item in userType"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="服务时间" required>
              <div style="display:flex;">
                <el-form-item prop="service_start_time">
                  <el-time-picker
                    @change="timestart"
                    placeholder="开始时间"
                    v-model="detail.service_start_time"
                    style="width: 100%;"
                    value-format="HH:mm"
                  ></el-time-picker>
                </el-form-item>

                <el-col class="line" style="text-align:center;" :span="1"
                  >-</el-col
                >

                <el-form-item prop="service_end_time">
                  <el-time-picker
                    @change="timeend"
                    placeholder="结束时间"
                    v-model="detail.service_end_time"
                    style="width: 100%;"
                    value-format="HH:mm"
                  ></el-time-picker>
                </el-form-item>
              </div>
            </el-form-item>

            <el-form-item label="账号" prop="account">
              <el-input v-model="detail.account" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input
                v-model="detail.password"
                class="password"
                type="password"
              ></el-input>
            </el-form-item>

            <el-form-item style="height:100px" label="头像" prop="head_pic">
              <div>
                <el-upload
                  list-type="picture-card"
                  :on-remove="handleRemove"
                  class="avatar-uploader"
                  :action="uploadPath"
                  :on-success="handSuccess"
                  :limit="1"
                  :file-list="filelist"
                  :on-exceed="handleExceed"
                  :before-upload="beforeAvatarUpload"
                  :data="{ store: 'customer_avatars' }"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>
              </div>
              <el-dialog>
                <img width="100%" :src="imageUrl" />
              </el-dialog>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="detail.remark"
                class="address"
                type="textarea"
                :rows="2"
              ></el-input>
            </el-form-item>

            <el-form-item label="账号状态">
              <el-switch
                v-model="detail.status"
                :active-value="1"
                :inactive-value="0"
                active-color="#03A98E"
              >
              </el-switch>
            </el-form-item>

            <el-form-item>
              <div>
                <el-button type="primary" @click="onSubmit">保存</el-button>
                <el-button @click="$router.back()">取消</el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { addCust } from "@/api/user/userList";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { uploadPath } from "@/api/currency";

export default {
  data() {
    var checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(+value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (phoneReg.test(value)) {
            callback();
          } else {
            callback(new Error("电话号码格式不正确"));
          }
        }
      }, 100);
    };

    return {
      detail: {
        head_pic: "",
        service_start_time: "",
        service_end_time: ""
      },
      uploadPath,
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [
          { required: true, message: "请输入号码", trigger: "blur" },
          { validator: checkPhone, message: "号码格式错误", trigger: "change" }
        ],
        head_pic: [{ required: true, message: "请上传头像", trigger: "blur" }],
        cust_role: [
          { required: true, message: "请选择客户角色", trigger: "blur" }
        ],
        service_start_time: [
          {
            type: "string",
            required: true,
            message: "请选择开始时间",
            trigger: "change"
          }
        ],
        service_end_time: [
          {
            type: "string",
            required: true,
            message: "请选择结束时间",
            trigger: "change"
          }
        ]
      },
      userType: [],
      filelist: [],
      imageUrl: ""
    };
  },
  mounted() {
    this.userType = JSON.parse(this.$route.query.custype);
  },
  methods: {
    handSuccess(res, file) {
      this.imageUrl = res.data.src;
      this.detail.head_pic = res.data.path;
    },
    //上传图片
    beforeAvatarUpload(file) {
      console.log(file);
      let type = file.type;
      let typeList = ["image/jpeg", "image/jpg", "image/png"];
      let isType = false;
      typeList.forEach(e => {
        if (e == type) {
          isType = true;
          return;
        }
      });
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isType) {
        this.$message.error("请上传正确的图片格式");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isType && isLt2M;
    },
    // 上传图片-删除
    handleRemove(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning("只能上传一张图片");
    },
    timestart(event) {
      console.log(event);
      this.detail.service_start_time = event;
    },
    timeend(event) {
      console.log(event);
      this.detail.service_end_time = event;
    },
    //保存按钮
    onSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let sz = JSON.parse(JSON.stringify(this.detail));
          let { pic, ...detail } = sz;
          console.log(this.detail);
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
              )
            },
            apiUrl().name,
            detail
          );
          console.log(detail);
          addCust(data).then(res => {
            if (res.errcode == 0) {
              setTimeout(() => {
                this.$message.success("创建成功");
              }, 300);
              this.$router.back(-1);
            } else {
              this.$message.error(res.errmsg);
            }
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.el-form-item__content > .el-input {
  width: 350px;
}
/deep/.el-select {
  width: 114px;
  .el-input__inner {
    width: 100% !important;
  }
}
.btns {
  margin: 20px 110px 0;
  button {
    margin-right: 10px;
  }
}
.address {
  max-width: 350px;
}
.flex {
  display: flex;
}
.flex-margin {
  padding: 0px 10px 0px 0px;
}
/deep/.el-select {
  width: 150px;
}

/deep/.avatar-uploader {
  cursor: pointer;
  width: 100px;
  height: 100px;
}
/deep/.el-upload {
  width: 100px;
  height: 100px;
  display: flex;
  align-items: center;
  border: 1px dashed #d9d9d9;
  justify-content: center;
}
/deep/.el-upload-list__item {
  width: 100%;
  height: 100%;
}
/deep/.el-upload-list__item:first-child {
  margin-left: 114px;
}
.el-upload .avatar {
  width: 100%;
  height: auto;
  display: block;
  padding: 0px 5px;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px dashed #c0ccda;
}
</style>
